import React, { Component } from 'react';
import { List, InputItem, WingBlank, WhiteSpace, Button,Toast } from "antd-mobile";
import formWrapper from '../../components/formWrapper'
import axios from 'axios'

@formWrapper
class Login extends Component {
  constructor(props){
    super(props)
    this.handleLogin=this.handleLogin.bind(this)
  }
  handleLogin(){
    let {pwd,user} = this.props.state
    if(!user||!pwd){
      Toast.info('用户名密码不能为空')
      return
    }
    axios.post('/api/user/login',{pwd,user}).then(res=>{
      Toast.info(res.data.msg)
      if(res.data.success){
        localStorage.setItem('token',res.data.data)
        this.props.history.push('/')
      }
    })
  }
  render(){
    return(
      <div>
        <WingBlank>
          <List>
            <InputItem onChange={v=>this.props.handleChange('user',v)}>
              用户名
            </InputItem>
            <InputItem type="password" onChange={v=>this.props.handleChange('pwd',v)}>
              密码
            </InputItem>
            <WhiteSpace></WhiteSpace>
            <Button type="primary" onClick={this.handleLogin}>登陆</Button>
            <WhiteSpace></WhiteSpace>
            <Button type="primary" onClick={()=>this.props.history.push('/register')}>注册</Button>
          </List>
        </WingBlank>
      </div>
    )
  }
}

export default Login